1778F - Maximizing Root - CodeForces Solution


dfs and similar dp graphs math trees

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>

using namespace std;

const int INF = 1e9 + 5;

int t, n, k, rt, divcnt, ans;

vector<int> arr;
vector<vector<int>> divs, tree, dp;

void dfs(int v, int p){
    if(tree[v].size() == 1 && v != 1){
        for(int i = 0; i < divcnt; i++){
            if(arr[v] % divs[rt][i] == 0) dp[v][i] = 0;
        }
    }
    else{
        fill(dp[v].begin(), dp[v].end(), 0);

        for(auto e : tree[v]){
            if(e != p){
                dfs(e, v);

                for(int i = 0; i < divcnt; i++){
                    if(arr[v] % divs[rt][i] != 0) dp[v][i] = -1;

                    if(dp[v][i] != -1){
                        int mn = INF;

                        for(int j = i; j < divcnt; j++){
                            if(divs[rt][j] % divs[rt][i] == 0 && dp[e][j] != -1){
                                mn = min(mn, dp[e][j]);
                            }
                        }

                        if(mn == INF) dp[v][i] = -1;
                        else dp[v][i] += mn;
                    }
                }
            }
        }
    }

    if(v != 1){
        vector<int> newdp(divcnt, INF);

        for(int i = 0; i < divcnt; i++){
            if(dp[v][i] != -1){
                for(int j = 0; j < divcnt; j++){
                    if((divs[rt][i] * divs[rt][i]) % divs[rt][j] == 0){
                        newdp[j] = min(newdp[j], dp[v][i] + 1);
                    }
                }
            }
        }

        for(int i = 0; i < divcnt; i++){
            if(newdp[i] != INF){
                if(dp[v][i] == -1) dp[v][i] = newdp[i];
                else dp[v][i] = min(dp[v][i], newdp[i]);
            }
        }
    }
}

void solve(){
    cin >> n >> k;

    arr.assign(n + 1, 0);
    tree.assign(n + 1, {});

    for(int i = 1; i <= n; i++) cin >> arr[i];

    rt = arr[1];
    divcnt = divs[rt].size();

    for(int i = 0, a, b; i < n - 1; i++){
        cin >> a >> b;

        tree[a].push_back(b);
        tree[b].push_back(a);
    }

    dp.assign(n + 1, vector(divcnt, -1));

    dfs(1, 0);

    ans = 0;

    for(int i = divcnt - 1; i >= 0; i--){
        if(dp[1][i] != -1){
            if(dp[1][i] <= k - 1) ans = max(ans, rt * divs[rt][i]);
            else if(dp[1][i] <= k) ans = max(ans, rt);
        }
    }

    cout << ans << "\n";
}

int main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);

    divs.assign(1000 + 1, {});

    for(int i = 1; i <= 1000; i++){
        for(int j = 1; j <= i; j++){
            if(i % j == 0) divs[i].push_back(j);
        }
    }

    cin >> t;

    while(t--){
        solve();
    }
}


Comments

Submit
0 Comments
More Questions

983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST
445. Add Two Numbers II
442. Find All Duplicates in an Array
437. Path Sum III